package Common;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class FontB {
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public static final int CENTER = 2;
    public static final byte SMALL = 0;
    public static final byte BIG = 1;
    private static Image imgFont;
    private static final String stMapBig="0123456789 ";
    private static final String stMapSmall = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.< ";
    private static final String stMap = "0123456789.,:!?()+-*/#$%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int[] cw = { 32, 32, 32, 30, 30, 30, 31, 30, 31,30, 32, 3, 2, 2, 2, 5, 6, 6, 7, 4, 10, 10, 9, 9, 9, 5, 6, 4, 6, 5, 4, 6, 6, 2, 3, 5, 2, 8, 6, 6, 6, 6, 3, 4, 3, 7, 6, 8, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 6, 7, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 2, 2, 4, 2, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 7, 7, 6, 6, 7, 5, 5, 7, 7, 4, 5, 6, 5, 10, 6, 7, 6, 7, 7, 6, 6, 7, 6, 10, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 6, 5, 5, 5, 5, 4, 4, 4, 5, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 6, 8, };
    //private static final String stMap="123456789*+~";
    private static int ch = 27;
    private static final int[] cw_B ={23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23};
    static {
        // cw[stMap.indexOf('a')] = 5;
        // cw[stMap.indexOf('á')] = 5;
        // cw[stMap.indexOf('à')] = 5;
        // cw[stMap.indexOf('ả')] = 5;
        // cw[stMap.indexOf('ã')] = 5;
        // cw[stMap.indexOf('ạ')] = 5;
        // cw[stMap.indexOf('s')] = 4;
        // cw[stMap.indexOf('t')] = 3;
        // cw[stMap.indexOf('c')] = 4;
        // cw[stMap.indexOf('k')] = 5;
        // //
        // cw[stMap.indexOf('m')] = 8;
        // cw[stMap.indexOf('M')] = 10;
        // cw[stMap.indexOf('r')] = 3;
        // cw[stMap.indexOf('u')] = 7;
        // cw[stMap.indexOf('ú')] = 7;
        // cw[stMap.indexOf('ù')] = 7;
        // cw[stMap.indexOf('ủ')] = 7;
        // cw[stMap.indexOf('ũ')] = 7;
        // cw[stMap.indexOf('ụ')] = 7;
        // cw[stMap.indexOf('ư')] = 7;
        // cw[stMap.indexOf('ứ')] = 7;
        // cw[stMap.indexOf('ừ')] = 7;
        // cw[stMap.indexOf('ử')] = 7;
        // cw[stMap.indexOf('ữ')] = 7;
        // cw[stMap.indexOf('ự')] = 7;
        // cw[stMap.indexOf('e')] = 5;
        // cw[stMap.indexOf('é')] = 5;
        // cw[stMap.indexOf('è')] = 5;
        // cw[stMap.indexOf('ẻ')] = 5;
        // cw[stMap.indexOf('ẽ')] = 5;
        // cw[stMap.indexOf('ẹ')] = 5;
        // cw[stMap.indexOf('ê')] = 5;
        // cw[stMap.indexOf('ế')] = 5;
        // cw[stMap.indexOf('ề')] = 5;
        // cw[stMap.indexOf('ể')] = 5;
        // cw[stMap.indexOf('ễ')] = 5;
        // cw[stMap.indexOf('ệ')] = 5;
        // System.out.println("");
        // cw[stMap.indexOf('#')] = 9;
        // cw[stMap.indexOf('$')] = 9;
        // System.out.print("{");
        // for (int i = 0; i < cw.length; i++)
        // System.out.print(cw[i] + ",");
        // System.out.println("}");
        for (int i = 0; i < cw.length; i++) {
            cw[i] += 1;
            // if (cw[i] > 11) cw[i] = 11;
        }
//        try {
//        	
//            imgFont = Image.createImage("/font/BigWhite.png");;
//            System.out.println("Font load error!"+ imgFont);
//        } catch (Exception e) {
//            System.out.println("Font load error!");
//        }
    }

    public static int getWidthOf(String st) {
        int pos;
        int len = 0;
        for (int i = 0; i < st.length(); i++) {
            pos = stMap.indexOf(st.charAt(i));
            if (pos == -1) pos = 0;
            len += (cw[pos]);
        }
        return len;
    }
    
    public static int getWidth(String st,int type[],String strMap) {
        int pos;
        int len = 0;
       
        for (int i = 0; i < st.length(); i++) {
        	
            pos = strMap.indexOf(st.charAt(i));
            if (pos == -1) pos = 0;
            len += (type[pos]);
        }
        return len;
    }
    public static int GetLength(int n)
    {
    	int len=0;
    	for(int i=0;i<n;i++)
    	{
    		len+=cw[i];
    	}
    	return len;
    }

    public static void drawString(Graphics g, String st, int x, int y, int align,byte type,int flip) {
        int pos;
        int x1;
        int []typeB= new int[cw.length];
        String str="";
        if(type==BIG)
        {
        	typeB=cw;
        	ch=27;//height
        	str=stMapBig;
        	imgFont=Util.fBigWhite;
        }
        else if(type==SMALL)
        {
        	typeB=cw_B;  
        	ch=21;//height
        	str=stMapSmall;
        	imgFont=Util.fRed;
        }
        int len = st.length();
        if (align == 0) x1 = x;
        else if (align == 1) x1 = x - getWidth(st,typeB,str);
        else
            x1 = x - (getWidth(st,typeB,str) >> 1);
        for (int i = 0; i < len; i++) {       	
            pos = str.indexOf(st.charAt(i));
            if (pos == -1) pos = 0;
    
            if (pos > -1)g.drawRegion(imgFont,0,pos * ch + 1,typeB[pos]-1, ch-1, flip, x1, y, 20);
            	//g.drawRegion(imgFont,0,pos * ch + 1,typeB[pos], ch-1, 0, x1, y, 20);
//            if (pos > -1)
//            {
//            	//g.drawRegion(imgFont, GetLength(pos),0, cw[pos], ch, 0, x1, y, 20);
//            	g.drawRegion(imgFont,0 ,GetLength(pos),23, ch, 0, x1, y, 20);
//            }            	
            x1 += typeB[pos]-8;           
        }
    }

    /**
     * Split string to multi line
     * 
     * @param src
     *            Source String
     * @param lineWidth
     *            Line Width In Pixel
     * @return List of String in Each Line
     */
    public static String[] splitStrInLine(String src, int lineWidth) {
        Vector list = new Vector();
        int srclen = src.length();
        String tem = "";
        int start = 0, end = 0;
        while (true) {
            while (getWidthOf(tem) < lineWidth) {
                tem += src.charAt(end);
                end++;
                if (end == srclen - 1) break;
            }
            if (end != srclen - 1 && (src.charAt(end + 1) != ' ')) {
                int endAnyway = end;
                while (true) {
                    if (src.charAt(end + 1) == ' ' && src.charAt(end) != ' ') break;
                    if (end == start) break;
                    end--;
                }
                if (end == start) end = endAnyway;
            }
            list.addElement(src.substring(start, end + 1));
            if (end == srclen - 1) break;
            start = end + 1;
            while (start != srclen - 1 && src.charAt(start) == ' ')
                start++;
            if (start == srclen - 1) break;
            end = start;
            tem = "";
        }
        String[] strs = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            strs[i] = (String) list.elementAt(i);
        }
        return strs;
    }
}
